package org.raymond.iworks.study.performance.gc;

/**
 * Recommended Use Cases for G1
 *      The first focus of G1 is to provide a solution for users running applications that require large heaps with limited GC latency.
 * This means heap sizes of around 6GB or larger, and stable and predictable pause time below 0.5 seconds.
 *      Applications running today with either the CMS or the ParallelOld garbage collector would benefit switching to G1
 * if the application has one or more of the following traits.
 *
 * More than 50% of the Java heap is occupied with live data.
 * The rate of object allocation rate or promotion varies significantly.
 * Undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second)
 */

// -Xms5M -Xmx20M -XX:+DisableExplicitGC -XX:+PrintCommandLineFlags -XX:+HeapDumpOnOutOfMemoryError
// -Xlog:gc*:/Users/raymond/workspaces/temp/gc_%t.log:time:filecount=10:filesize=10m
    // -XX:G1HeapRegionSize=10m
public class GCDemo {
    public static void main(String[] args) {
        test1();
    }

    // -Xms5M -Xmx20M -Xlog:gc*
    public static void test1(){
        System.out.println("enter main");
        printMemory();
        GCDemo demo = new GCDemo();
        System.out.println("new demo");
        printMemory();
        while(true) {
            byte[] bytes = new byte[1 * 1024 * 1024];
            System.out.println("allocate 1024KB");
            printMemory();

            bytes = new byte[4 * 1024 * 1024];
            System.out.println("allocate 4096KB");
            printMemory();
        }
    }

    public static void printMemory(){
        System.out.println();
        System.out.println("Max memory = " + Runtime.getRuntime().maxMemory() / 1024+ " KB");
        System.out.println("Free memory = "+Runtime.getRuntime().freeMemory()/1024+ " KB");
        System.out.println("Total memory = " + Runtime.getRuntime().totalMemory()/ 1024+ " KB");
        System.out.println();
    }
}